﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace Util
{
    public class Email
    {
        private string user = "";
        private string password = "";
        private string smtp = "smtp.gmail.com";
        private int port = 587;


        public void send(string from , string to , string subject , string body,string pathFile)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient( this.smtp );
                mail.From = new MailAddress(from);
                mail.To.Add(to);
                mail.Subject = subject;
                mail.Body = body;

                if (pathFile != null && pathFile.Trim().Length > 0)
                {
                    Attachment attachment  = new Attachment( pathFile );
                    mail.Attachments.Add(attachment);
                }

                SmtpServer.Port = this.port;
                SmtpServer.Credentials = new System.Net.NetworkCredential( this.user, this.password );
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

            }
            catch (Exception e)
            {
                Console.WriteLine("Exception -->> ", e.ToString());
            }

        }

        public void send(string from, string []to, string subject, string body, string pathFile)
        {
            try
            {

                if ( to!= null && to.Length > 0)
                {
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient( this.smtp );
                    mail.From = new MailAddress(from);

                    foreach(string item in to){
                        mail.To.Add( item );
                    }

                    mail.Subject = subject;
                    mail.Body = body;

                    if (pathFile != null && pathFile.Trim().Length > 0)
                    {
                        Attachment attachment = new Attachment(pathFile);
                        mail.Attachments.Add(attachment);
                    }

                    SmtpServer.Port = this.port;
                    SmtpServer.Credentials = new System.Net.NetworkCredential(this.user, this.password);
                    SmtpServer.EnableSsl = true;

                    SmtpServer.Send(mail);
                
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Exception -->> ", e.ToString());
            }

        }



        public void execute()
        {
            this.send("ernesto_lopez_h2@hotmail.com", "ernesto_lopez_h2@hotmail.com", "Pruebas...", "Factura electronica",null);
        }

    }
}